package edu.njust.cma.service.impl;

import edu.njust.cma.DTO.ProjectStandard;
import edu.njust.cma.mapper.*;
import edu.njust.cma.pojo.*;
import edu.njust.cma.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    TaskMapper taskMapper;

    @Autowired
    StandardMapper standardMapper;

    @Autowired
    SmallProjectMapper smallProjectMapper;

    @Autowired
    SubmissionMapper submissionMapper;
    @Override
    public Project getProject(Integer id) {
        return projectMapper.getProject(id);
    }

    @Override
    public List<ProjectStandard> getAllProject() {
        List<Project> projectList = projectMapper.getAllProject();
        List<ProjectStandard> projectStandardList = new ArrayList<>();
        for (Project project : projectList) {
            List<Integer> standardIdList = smallProjectMapper.getStandardIdByProjectId(project.getId());
            List<Standard> standardList = new ArrayList<>();

            for (Integer standardId : standardIdList) {
                standardList.add(standardMapper.getStandardById(standardId));
            }
            Integer TaskCount = taskMapper.getCountTaskByProjectId(project.getId());
            System.out.println(project.getId());
            System.out.println(TaskCount);
            Integer TaskCompleteCount = taskMapper.getCountTaskCompleteByProjectId(project.getId());
            projectStandardList.add(new ProjectStandard(project, TaskCount,TaskCompleteCount, standardList));
        }
        return projectStandardList;
    }

    @Override
    public Integer getCountTask(Integer projectId) {
        List<Integer> smallProjectIdList = smallProjectMapper.getSmallProjectId(projectId);
        int count = 0;
        for(Integer smallProjectId : smallProjectIdList){
            count += taskMapper.getCountTaskBySmallProjectId(smallProjectId);
        }
        return count;
    }

    @Override
    public Integer getCountTaskComplete(Integer projectId) {
        List<Integer> smallProjectIdList = smallProjectMapper.getSmallProjectId(projectId);
        int count = 0;
        for(Integer smallProjectId : smallProjectIdList){
            count += taskMapper.getCountTaskCompleteBySmallProjectId(smallProjectId);
        }
        return count;
    }
}
